/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.stock.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.stock.domain.TblStockInfo;
import me.zhengjie.stock.domain.vo.TblStockInfoQueryCriteria;
import me.zhengjie.stock.mapper.TblStockInfoMapper;
import me.zhengjie.stock.service.TblStockInfoService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dolojia
 * @description 服务实现
 * @date 2024-05-04
 **/
@Service
@RequiredArgsConstructor
public class TblStockInfoServiceImpl extends ServiceImpl<TblStockInfoMapper, TblStockInfo> implements TblStockInfoService {

    private final TblStockInfoMapper tblStockInfoMapper;

    @Override
    public PageResult<TblStockInfo> queryAll(TblStockInfoQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(tblStockInfoMapper.findAll(criteria, page));
    }

    @Override
    public List<TblStockInfo> queryAll(TblStockInfoQueryCriteria criteria) {
        return tblStockInfoMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TblStockInfo resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TblStockInfo resources) {
        TblStockInfo tblStockInfo = getById(resources.getId());
        tblStockInfo.copy(resources);
        saveOrUpdate(tblStockInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TblStockInfo> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TblStockInfo tblStockInfo : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("股票代码", tblStockInfo.getCode());
            map.put("股票名称", tblStockInfo.getName());
            map.put("市场（0:深证A股，1:上证A股）", tblStockInfo.getMarket());
            map.put("行业", tblStockInfo.getIndustry());
            map.put("城市", tblStockInfo.getCity());
            map.put("股票状态（0:）", tblStockInfo.getOpenStatus());
            map.put(" gmtCreate", tblStockInfo.getGmtCreate());
            map.put(" gmtModified", tblStockInfo.getGmtModified());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}